<template>
<div>
  <xdh-form ref="refform" :footerLoading="isDisabled" :edits="true" :model="formData" resetText="取消" :fields="fieldValue" @submit="submitForm" @reset="alertReset" footerAlign="right" :footer="isFooter" @refreshInit="refreshInit">
 </xdh-form>
 
</div>
</template>

<script>
import XdhForm from '@/widgets/xdh-form'
import { getInfo } from '@/api/tpl'
import { commonGetInfo } from '@/api/common'
import analysisRouterParam from '@/utils/analysis-router-param.js'

export default {
  name:'alertforms',
  components: { XdhForm },
  props:{
      data:{
          type:[Object,Array],
          default:()=>{}
      }
  },    
  data() {
    return {
      formData: {},
      tab_id: 0,
      isDisabled:false,
      index_url:'',
      ajax_url:'',
      htmlList:[],
      isFooter:true
    }
  },
  mounted() {
    
  },
  computed: {
      fieldValue: {
        get() {
            this.index_url = this.$attrs.index_url;
            this.isFooter = this.$attrs.is_footer;
            this.ajax_url = this.$attrs.ajax_url;
            this.htmlList = this.$attrs.template_content[0].content;
            // this.htmlList = this.$attrs.template_content;
            this.getDatas();
            return this.htmlList;
        },
        set(val) {
          this.$emit('input', val)
        }
      }
  },
  methods: {
    refreshInit(){
      this.inits();
    },
    async getDatas() {
      
        if(!this.index_url) {
          return false;
        }
      var routerUrl = analysisRouterParam.init(this.index_url,this.data);
     console.log(this.index_url,this.data);
      const res = await commonGetInfo(routerUrl.path,routerUrl.query);
  
      if (res && res.code === 200) {
        var that = this;
        if (res.data.info && JSON.stringify(res.data.info) != '{}' && JSON.stringify(res.data.info) != '[]') {
          that.formData = res.data.info;
        }

          this.htmlList.forEach((elem) => {
            if(that.formData[elem.prop] === undefined) {
              if (!elem.value) {
                if (elem.props.value === false) {
                  elem.value = false;
                } else if (elem.props.value === 0) {
                  elem.value = 0;
                } else if (elem.props.value === '') {
                  elem.value = '';
                } else {
                  elem.value = elem.props.value ? elem.props.value : null;
                }
              } 
              this.$set(that.formData, elem.prop, elem.value);
            }else if (typeof elem.value == 'object'){
              elem.value = that.formData[elem.prop];
            }
              
          });

          console.log(that.formData,this.htmlList,'+++++++++++++')

      }
    },
    async submitForm(formDatas) {
      this.isDisabled = true;
      var d = JSON.parse(JSON.stringify(formDatas));
      var da = JSON.parse(JSON.stringify(this.data));
      d = Object.assign(d,da);
      console.log(d,this.data);
      // var d = Object.assign(formDatas,this.data);
      const res = await commonGetInfo(this.ajax_url, d);
      if(res && res.code === 200) {
        this.isDisabled = false;
        this.$message({
          message: res.msg,
          type: 'success'
        });
        
        this.$emit('submit',{});
        return true;
      }

      this.isDisabled = false;
      return true;
    },
    alertReset(){
       this.$emit('reset',{});
    },
    getkey() {
      return Math.random();
    }
  }
}
</script>
